package cn.sdfykjyxgs.synclient.util;

import cn.sdfykjyxgs.synclient.config.WebSocketServer;
import cn.sdfykjyxgs.syncommon.entity.FileChangeLog;
import cn.sdfykjyxgs.syncommon.socket.WebSocketMessage;

import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

public class MyFileLogQuery {
    //元素集合
    public static List<FileChangeLog> list = Collections.synchronizedList(new ArrayList<>());
    //计数器(同步),判断集合元素数量
    public static AtomicInteger count = new AtomicInteger();
    //过滤 FileChangeLog.maxRetryCount小于5
    public static long maxRetryCount = 5;
    //集合上限与下限,final必须指定初值
    private static final int minSize = 0;
    private static final int maxSize = 9999999;
    //    //构造器指定最大值
//    public MyFileLogQuery(int maxSize) {
//        this.maxSize = maxSize;
//    }
    //初始化对象,用于加锁,也可直接用this
    private static Object lock = new Object();

    //put方法:往集合中添加元素,如果集合元素已满,则此线程阻塞,直到有空间再继续
    public static void put(FileChangeLog log) {
        synchronized (lock) {
            while (count.get() == maxSize) {
                try {
                    lock.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            list.add(log);
            //计数器加一
            count.incrementAndGet();
//            System.out.println("放入元素:" + log);
            //发送进度消息
            ArrayList<FileChangeLog> logs = new ArrayList<>();
            logs.add(log);
            WebSocketServer.sendMessage(new WebSocketMessage("", "10003", logs));
            //唤醒另一个线程,(处理极端情况:集合一开始就是空,此时take线程会一直等待)
            lock.notify();
        }
    }

    //take方法:从元素中取数据,如果集合为空,则线程阻塞,直到集合不为空再继续
    public static FileChangeLog take() {
        FileChangeLog result = null;
        synchronized (lock) {
            while (count.get() == minSize) {
                try {
                    lock.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            //移除第一个
//            result = list.removeFirst();
            result = list.remove(0);
            //计数器减一
            count.decrementAndGet();
//            System.out.println("拿走元素:" + result);
            //唤醒另一个线程,(处理极端情况:集合一开始就是满的,此时put线程会一直等待)
            lock.notify();
        }
        return result;
    }

    public static FileChangeLog get() {
        FileChangeLog result = null;
        synchronized (lock) {
            while (count.get() == minSize) {
                try {
                    lock.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            //移除第一个
//            result = list.getFirst();
            result = list.get(0);
//            System.out.println("获取 get id" + result.getId());
            //唤醒另一个线程,(处理极端情况:集合一开始就是满的,此时put线程会一直等待)
            lock.notify();
        }
        return result;
    }

    public static boolean getRemove() {
        FileChangeLog take = take();
        return take == null ? false : true;
    }

    public int getSize() {
        return this.count.get();

    }

    public static void initMaxRetryCount(long num){
        maxRetryCount=num;
    }
}
